package com.monitor.util.command;

import java.util.ArrayList;
import java.util.List;

public class CommandFactory {

	/**
	 * 创建可执行的命令
	 * @param cmdStr
	 * @return
	 * @throws Exception
	 */
	public static ISysCommand createCommand(String cmdStr) throws Exception{
		String os = System.getProperty("os.name"); 
		if (os != null && os.startsWith("Windows")) {
			throw new Exception("不支持windows命令");
		}else{
			return new UnixCommand(cmdStr);
		}
	}
	
	public static int getProcessAmountByName(String processName) throws Exception{
		ISysCommand processStr = CommandFactory.createCommand("ps -ef ");
		List<String> resultList = processStr.getLineStrList();
		
		if(resultList == null || resultList.size() == 0)return 0;
		
		int amount = 0;
		for(int i = 0; i < resultList.size(); i ++){
			String lineStr = resultList.get(i);
			
			if(lineStr != null && lineStr.indexOf(processName) != -1){
				amount += 1;
			}
		}
		
		return amount;
	}
	
	public static List<String> getProcessInfoByName(String processName) throws Exception{
		ISysCommand processStr = CommandFactory.createCommand("ps -ef ");
		List<String> resultList = processStr.getLineStrList();
		
		List<String> processInfoList = new ArrayList<String>();
		
		if(resultList == null || resultList.size() == 0)return processInfoList;
		
		for(int i = 0; i < resultList.size(); i ++){
			String lineStr = resultList.get(i);
			
			if(lineStr != null && lineStr.indexOf(processName) != -1){
				processInfoList.add(lineStr);
			}
		}
		
		return processInfoList;
	}
	
	public static String getCopyCommand(String srcPath, String distPATH) throws Exception{
		String os = System.getProperty("os.name"); 
		if (os != null && os.startsWith("Windows")) {
			throw new Exception("不支持windows命令");
		}else{
			return CommandStrUtil.getUnixCopyCmd(srcPath, distPATH);
		}
	}
	/**
	 * @param args
	 * @description 用一句话说明这个方法做什么
	 * @version 1.0
	 * @author 黄丽君
	 * @throws Exception 
	 * @update 2013-10-10 下午11:48:10
	 */
	public static void main(String[] args) throws Exception {
//		String commandStr = "wget -rq -k -l 1  -t 3 -T 10  -P E:/monitor/base/ -N -i E:/monitor/0_url.conf -U \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20100101 Firefox/23.0\"";
//
//        ISysCommand command = CommandFactory.createCommand(commandStr);
//        command.excute();
//		ISysCommand processStr = CommandFactory.createCommand("ps -ef ");
//		List<String> resultList = processStr.getLineStrList();
//		
//		for(int i = 0; i < resultList.size(); i ++){
//			String lineStr = resultList.get(i);
			System.out.println(CommandFactory.getProcessAmountByName("distortWget"));
//		}
	}

}
